package cn.summit.serialization.adapter;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

/**
 * @author summit
 * @since 2020/11/13 14:09
 */
public class LongSerializer implements JsonSerializer<Long> {

    public static final Long MAX_SAFE_INTEGER = 9007199254740991L;

    @Override
    public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
        if (src != null) {
            if (src > MAX_SAFE_INTEGER) {
                return new JsonPrimitive(src.toString());
            }else {
                return new JsonPrimitive(src);
            }
        }
        return null;
    }
}
